<%@ page import="com.bothin.experiment4.utils.NavSideUtil" %>
<%@ page import="com.bothin.experiment4.model.NavSideNode" %>
<%@ page import="java.util.List" %>
<%@ page import="com.bothin.experiment4.security.LoginUserUtil" %>
<%@ page import="com.bothin.experiment4.security.LoginUser" %>
<%@ page import="org.springframework.security.core.GrantedAuthority" %>
<%@ page import="java.util.Collection" %>
<%@ page import="java.util.Iterator" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String nav = request.getParameter("nav");
    int[] active = new int[2];
    if (nav != null) {
        String[] split = nav.split("#");
        for (int i = 0; i < split.length; i++) {
            active[i] = Integer.parseInt(split[i]);
        }
    }
    //获取侧导航栏数据
    List<NavSideNode> nodes = null;
    LoginUser loginUser = LoginUserUtil.getLoginUser();
    if (loginUser!=null){
        Collection<? extends GrantedAuthority> authorities = loginUser.getAuthorities();
        Iterator it = authorities.iterator();
        while (it.hasNext()) {
            String temp = it.next().toString();
            if (temp!=null){
                if (temp.equals("ROLE_USER_ADMIN")){
                    nodes = NavSideUtil.getNavSideNode();
                }else if (temp.equals("ROLE_USER_STOCKMAN")){
                    nodes = NavSideUtil.getStockmanNavSideNode();
                }else if (temp.equals("ROLE_USER_SALESMAN")){
                    nodes = NavSideUtil.getSalesmanNavSideNode();
                }
            }
        }
    }

//    System.out.println(LoginUserUtil.getLoginUser());
    request.setAttribute("nodes",nodes);
    request.setAttribute("active",active);
%>

<%--侧边菜单--%>
<div class="layui-side layui-side-menu">
    <div class="layui-side-scroll">
        <div class="layui-logo" href="${pageContext.request.contextPath}/">
            <span>bothin 销售管理系统</span>
        </div>

        <ul class="layui-nav layui-nav-tree" lay-shrink="all" id="LAY-system-side-menu" lay-filter="layadmin-system-side-menu">
            <c:forEach items="${nodes}" var="item" varStatus="status">
                <%--是否展开下拉菜单--%>
                <li class="layui-nav-item
                <c:if test="${status.count==active[0]}">
                     layui-nav-itemed
                </c:if>
                <c:if test="${status.count==active[0]&&active[1]==0}">
                     layui-this
                </c:if>
                ">
                    <a href="${pageContext.request.contextPath}${item.url}" lay-tips="${item.name}" lay-direction="2">
                        <i class="layui-icon ${item.icon}"></i>
                        <cite>${item.name}</cite>
                    </a>
                    <c:if test="${item.nodeList!=null}">
                        <dl class="layui-nav-child">
                            <c:forEach items="${item.nodeList}" var="child" varStatus="status_child">
                                <dd
                                <c:if test="${status.count==active[0]&&status_child.count==active[1]}">
                                    class="layui-this"
                                </c:if>
                                >
                                    <a href="${pageContext.request.contextPath}${child.url}" >${child.name}</a>
                                </dd>
                            </c:forEach>
                        </dl>
                    </c:if>
                </li>
            </c:forEach>
        </ul>
    </div>
</div>